/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The use of this code should not be permitted!
 * If you want to use, please do contact me!
 */
package MyIM.client.view.form;

import MyIM.client.engine.Engine;
import MyIM.client.view.lnf.MetroStyle;
import MyIM.client.view.lnf.SegoeFont;
import MyIM.client.view.lnf.Style;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class Dialog_Confirm extends JDialog implements ActionListener {

    private String msg;
    private int requester, requested, group;
    private Engine engine;

    public Dialog_Confirm(String msg) {
        this.msg = msg;

        init();
    }

    public Dialog_Confirm(Engine engine, String msg, int requester, int requested, int group) {
        this.msg = msg;
        this.requester = requester;
        this.requested = requested;
        this.group = group;
        this.engine = engine;
        init();
    }

    private void init() {
        // Initializes the frame
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        initLayout();
        setResizable(false);
        pack();
        setLocationRelativeTo(null);
    }

    private void initLayout() {

        Font title = MetroStyle.FORM_TITLE;
        Font head = SegoeFont.getSegoeUIFont("bold", 16.0f);
        Font body = SegoeFont.getSegoeUIFont("normal");

        JLabel label;
        JPanel panel;
        JButton btnOk = new JButton("Yup :D");
        btnOk.setActionCommand("ok");
        btnOk.addActionListener(this);
        btnOk.setPreferredSize(new Dimension(75, 28));
        btnOk.setFont(MetroStyle.BUTTON_FONT);
        getRootPane().setDefaultButton(btnOk);

        JButton btnCancel = new JButton("No :|");
        btnCancel.setActionCommand("cancel");
        btnCancel.addActionListener(this);
        btnCancel.setPreferredSize(new Dimension(75, 28));
        btnCancel.setFont(MetroStyle.BUTTON_FONT);

        setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();

        cons.anchor = GridBagConstraints.FIRST_LINE_START;
        cons.fill = GridBagConstraints.HORIZONTAL;

        // Adds components
        cons.gridx = 0;
        cons.gridy = 0;

        addText(cons, new Insets(1, 19, 1, 19), body, msg);

        panel = new JPanel(new FlowLayout());
        panel.add(btnOk);
        panel.add(btnCancel);

        cons.gridy++;
        cons.insets = new Insets(15, 5, 10, 5);
        add(panel, cons);
    }

    private void addText(GridBagConstraints cons, Font font, String text) {
        cons.gridy++;
        JLabel label = new JLabel(text);
        label.setFont(font);
        add(label, cons);
    }

    private void addText(GridBagConstraints cons, Insets insets, Font font,
            String text) {
        cons.insets = insets;
        addText(cons, font, text);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equalsIgnoreCase("ok")) {
            try {
                System.out.println("Em dong y =))");
                engine.getServer().addFriend(requester, requested, group);
                engine.getServer().acceptAddFriend(requested, requester);
            } catch (RemoteException ex) {
                Logger.getLogger(Dialog_Confirm.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                this.dispose();
            }
        } else if (cmd.equalsIgnoreCase("cancel")) {
            this.dispose();
        }
    }
}
